package com.team06.util;

import com.team06.pojo.Cane;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.File;

public class CaneXmlParserUtil {
    public static Cane parseXml(File xmlFile) {
        Cane cane = new Cane();
        try {
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(xmlFile);
            Element root = document.getRootElement();

            // 解析 META 节点
            Element meta = root.getChild("META");
            cane.setMetaSnDR(meta.getChildText("SNDR"));
            cane.setMetaRCVR(meta.getChildText("RCVR"));
            cane.setMetaSEQN(Integer.parseInt(meta.getChildText("SEQN")));
            cane.setMetaDDTM(meta.getChildText("DDTM"));
            cane.setMetaTYPE(meta.getChildText("TYPE"));
            cane.setMetaSTYP(meta.getChildText("STYP"));

            // 解析 DFLT 节点
            Element dflt = root.getChild("DFLT");
            cane.setDfltFLID(dflt.getChildText("FLID"));
            cane.setDfltFFID(dflt.getChildText("FFID"));
            cane.setDfltFIDE(dflt.getChildText("FIDE"));
            cane.setDfltFATT(dflt.getChildText("FATT"));
            cane.setDfltABST(dflt.getChildText("ABST"));
            cane.setDfltIAST(dflt.getChildText("IAST"));
            cane.setDfltABRS(dflt.getChildText("ABRS"));
            cane.setDfltIARS(dflt.getChildText("IARS"));
            cane.setDfltMABS(dflt.getChildText("MABS"));
            cane.setDfltMABR(dflt.getChildText("MABR"));
            cane.setDfltMIAS(dflt.getChildText("MIAS"));
            cane.setDfltMIAR(dflt.getChildText("MIAR"));

            return cane;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析 Cane XML 失败: " + xmlFile.getAbsolutePath(), e);
        }
    }
}